package com.hulk.ratel.manage.commonservice;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.hulk.ratel.common.constant.CommonConstants;
import com.hulk.ratel.common.constant.enums.YesOrNo;

import com.hulk.ratel.common.util.MathUtil;
import com.hulk.ratel.manage.bo.RouteReqBO;
import com.hulk.ratel.manage.bo.RouteRespBO;
import com.hulk.ratel.persistence.entity.PartnerOrgOverWeightRoute;
import com.hulk.ratel.persistence.repository.IPartnerOrgOverWeightRouteRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hulk
 * @E-mail:29572320@qq.com
 * @version Create on:  2017年5月26日 上午11:45:15
 * Class description  上游支付机构路由选择
 */

@Component
@Slf4j
public class DispatchRouteService {

    @Autowired
    private IPartnerOrgOverWeightRouteRepository partnerOrgOverWeightRouteRepository;

    public RouteRespBO dispatch(RouteReqBO reqBO,String workId) {
        log.info("支付机构路由,请求参数：{},workId: {}", reqBO,workId);

        RouteRespBO result;
        PartnerOrgOverWeightRoute param = new PartnerOrgOverWeightRoute();
        //param.setOverorgCode(reqBO.getOverOrgCode());
        param.setAuditedFlag(YesOrNo.YES.getValue());
        param.setUseFlag(YesOrNo.YES.getValue());
        param.setPartnerNo(reqBO.getPartnerNo());

        Wrapper<PartnerOrgOverWeightRoute> queryWeight;
        if (null != reqBO.getOrgCodeList() && reqBO.getOrgCodeList().size() > 0) {
            queryWeight = new QueryWrapper<>(param).in("org_code", reqBO.getOrgCodeList()).orderByDesc("id");
        } else {
            queryWeight = new QueryWrapper<>(param).orderByDesc("id");
        }

        log.debug("支付机构路由,查询支付机构末端支付机构路由规则表查询参数：{}，workId: {}", queryWeight,workId);
        List<PartnerOrgOverWeightRoute> powerList = partnerOrgOverWeightRouteRepository.list(queryWeight);
        log.debug("支付机构路由,查询支付机构末端支付机构路由规则表查询结果：{}，workId: {}", powerList,workId);
        //没有配置路由规则
        if (null == powerList || powerList.size() == 0) {
            log.info("支付机构路由,未查询到查询支付机构末端支付机构路由规则，空返回。workId: {}",workId);
            return null;
        }
        //只配置了一条路由规则
        if (powerList.size() == 1) {
            result = new RouteRespBO();
            result.setOrgCode(powerList.get(0).getOrgCode());
            log.info("支付机构路由,只查询到一条支付机构末端支付机构路由规则，直接返回 traceId: {}, result: {},workId: {}", reqBO.getTraceId(), result,workId);
            return result;
        }

        log.info("支付机构路由,查询到多条支付机构末端支付机构路由规则，进行权重分配：{}，workId: {}", powerList,workId);
        List<Integer> percentList = new ArrayList<>();
        int percent = 0;
        //配置多笔路由规则
        for (PartnerOrgOverWeightRoute route : powerList) {
            percent += route.getWeight();
            percentList.add(percent);
        }
        int num = MathUtil.getRandom(CommonConstants.ONE_INT, percent).intValue();
        String orgCode = "";
        for (int i = 0; i < percentList.size(); i++) {
            if (num <= percentList.get(i)) {
                orgCode = powerList.get(i).getOrgCode();
                break;
            }
        }
        result = new RouteRespBO();
        result.setOrgCode(orgCode);
        log.info("支付机构路由 traceId: {}, result: {},workId: {}", reqBO.getTraceId(), result,workId);
        return result;
    }

}
